<template>
  <pro-config-provide :config="configData">
    <div id="app" :class="{ 'is-component': isComponent }">
      <main-header v-if="lang !== 'play'"></main-header>
      <div class="main-cnt">
        <router-view></router-view>
      </div>
      <main-footer v-if="lang !== 'play' && !isComponent"></main-footer>
    </div>
  </pro-config-provide>
</template>

<script>
  import { use } from 'main/locale';
  import zhLocale from 'main/locale/lang/zh-CN';
  import enLocale from 'main/locale/lang/en';
  import esLocale from 'main/locale/lang/es';
  import frLocale from 'main/locale/lang/fr';
  import { getComponentByUrl } from './components/async-chunk/require-ensure'
  const lang = location.hash.replace('#', '').split('/')[1] || 'zh-CN';
  const localize = lang => {
    switch (lang) {
      case 'zh-CN':
        use(zhLocale);
        break;
      case 'es':
        use(esLocale);
        break;
      case 'fr-FR':
        use(frLocale);
        break;
      default:
        use(enLocale);
    }
  };
  localize(lang);

  export default {
    name: 'app',
    data(){
      return {
        // 测试数据
        configData:{
          //菜单数
          menuData: [],
          getLabelPropFromDetailModel: function(prop){
            return  `${prop}AsText`
          },
          //字典数据
          dictData: {
              YES_OR_NOT: [{name: '是', code: '1'}, {name: '否', code: '2'}, {name: '不确定', code: '3'}, {name: '未知', code: '4'}],
              SEX: [{name: '男', code: '1'}, {name: '女', code: '2'}],
              system: {
                YES_OR_NOT: [{name: '是-系统', code: '1'}, {name: '否-系统', code: '2'}, {name: '不确定', code: '3'}, {name: '未知', code: '4'}],
              }
          },
          getDataByDictType: null,
          formatText: {
            labelKey: 'name',
            codeKey: 'code',
            modelLabelFieldSuffix: ''
          },
          // 操作权限
          authorityData: [],
          // 用户信息
          useInfoData: {},
          //getComponentByUrl
          getComponentByUrl: getComponentByUrl,
          // 接口规范配置
          api: {
            //业务异常标记字段
            statusField: '',
            // 业务异常代码
            stateusCodeField: '',
            //业务异常描述
            messageField: '',
            //查询-total字段
            totalField: 'total',
            //每页多少条字段
            pageSizeField: 'pageSize',
            // 当前字段
            currentPageField: 'currentPage',
            // 默认查询接口响应结果字段
            dataField: ''
          }
        }
      }
    },
    computed: {
      lang() {
        return this.$route.path.split('/')[1] || 'zh-CN';
      },
      isComponent() {
        return /^component-/.test(this.$route.name || '');
      }
    },

    watch: {
      lang(val) {
        if (val === 'zh-CN') {
          this.suggestJump();
        }
        localize(val);
      }
    },

    methods: {
      suggestJump() {
        if (process.env.NODE_ENV !== 'production') return;

        const href = location.href;
        const preferGithub = localStorage.getItem('PREFER_GITHUB');
        const cnHref = href.indexOf('eleme.cn') > -1 || href.indexOf('element-cn') > -1 || href.indexOf('element.faas') > -1;
        //if (cnHref || preferGithub) return;
        //setTimeout(() => {
          //if (this.lang !== 'zh-CN') return;
          //this.$proConfirm('建议大陆用户访问部署在国内的站点，是否跳转？', '提示')
            //.then(() => {
             // location.replace('https://element.eleme.cn');
            //})
            //.catch(() => {
              //localStorage.setItem('PREFER_GITHUB', 'true');
            //});
        //}, 1000);
      }
    },

    mounted() {
      localize(this.lang);
      if (this.lang === 'zh-CN') {
        this.suggestJump();
      }

      getComponentByUrl('test-vue').then((res) => {
        console.log('-------------', res)
      })
    }
  };
</script>
